package com.wbu.estatemanagement.controller;
import com.wbu.estatemanagement.common.MessageConstant;
import com.wbu.estatemanagement.common.Result;
import com.wbu.estatemanagement.common.StatusCode;
import com.wbu.estatemanagement.domain.User;
import com.wbu.estatemanagement.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping("/userInfo")
    public Result userInfo(HttpSession httpSession, HttpServletResponse res,HttpServletRequest req) throws IOException {

        User sessionUser= (User) httpSession.getAttribute("user");
        if(sessionUser==null){
            return new Result(false, StatusCode.ERROR, MessageConstant.FAIL);
        }
        Integer userId = sessionUser.getId();
        User user=userService.findUser(userId);
            return new Result(true, StatusCode.OK, MessageConstant.SUCCESS,user);
    }

    @PostMapping("/update")
    public Result updatePassword(HttpSession httpSession, @RequestBody User userOne){
        User user = (User)httpSession.getAttribute("user");
        user.setPassword(userOne.getPassword());
        Integer i = userService.update(user);
        if (i<0){
            return new Result(false, StatusCode.ERROR, MessageConstant.FAIL);
        }
        return new Result(false, StatusCode.ERROR, MessageConstant.SUCCESS);
    }


}
